﻿using System;
using System.ServiceModel;

namespace Motar.ServiceModel
{
    
    /// <summary>
    /// 
    /// </summary>
    public static class ServiceHostExtensions
    {

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TServiceHost"></typeparam>
        /// <param name="serviceHost"></param>
        /// <returns></returns>
        public static TServiceHost ApplyInjectingBehavior<TServiceHost>(this TServiceHost serviceHost) where TServiceHost : ServiceHostBase
        {
            serviceHost.Opening += new EventHandler((sender, e) =>
            {
                if (serviceHost.Description.Behaviors.Find<InjectingServiceBehavior>() == null)
                {
                    serviceHost.Description.Behaviors.Add(new InjectingServiceBehavior());
                }
            });

            return serviceHost;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TServiceHost"></typeparam>
        /// <param name="serviceHost"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static TServiceHost ApplyInjectingBehavior<TServiceHost>(this TServiceHost serviceHost, string key) where TServiceHost : ServiceHostBase
        {
            serviceHost.Opening += new EventHandler((sender, e) =>
            {
                if (serviceHost.Description.Behaviors.Find<InjectingServiceBehavior>() == null)
                {
                    serviceHost.Description.Behaviors.Add(new InjectingServiceBehavior(key));
                }
            });

            return serviceHost;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TServiceHost"></typeparam>
        /// <param name="serviceHost"></param>
        /// <returns></returns>
        public static TServiceHost ApplyExceptionHandlingBehavior<TServiceHost>(this TServiceHost serviceHost) where TServiceHost : ServiceHostBase
        {
            serviceHost.Opening += new EventHandler((sender, e) =>
            {
                if (serviceHost.Description.Behaviors.Find<ExceptionHandlingServiceBehavior>() == null)
                {
                    serviceHost.Description.Behaviors.Add(new ExceptionHandlingServiceBehavior());
                }
            });

            return serviceHost;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TServiceHost"></typeparam>
        /// <param name="serviceHost"></param>
        /// <returns></returns>
        public static TServiceHost ApplyExceptionHandlingBehavior<TServiceHost>(this TServiceHost serviceHost, string key) where TServiceHost : ServiceHostBase
        {
            serviceHost.Opening += new EventHandler((sender, e) =>
            {
                if (serviceHost.Description.Behaviors.Find<ExceptionHandlingServiceBehavior>() == null)
                {
                    serviceHost.Description.Behaviors.Add(new ExceptionHandlingServiceBehavior(key));
                }
            });

            return serviceHost;
        }
    }
}
